module CopperWire
  def self.append_features(base)
    super
    base.after_filter :show_runtimes
    base.around_filter { |controller, action| controller.total_runtime = Benchmark.measure(&action).real }
  end  
end

module ActionController
  class Base
    attr_accessor :total_runtime    
    
    def show_runtimes      
      return if response.headers['Status'] == '304 Not Modified' || !(response.body && response.body.respond_to?(:sub!))  
      db_runtime = ((0 + (@db_rt_before_render || 0) + (@db_rt_after_render || 0)) * 1000).truncate
      rendering_runtime = ((@rendering_runtime || 0) * 1000).truncate
      total_runtime = ((@total_runtime || 0) * 1000).truncate
      response.body.gsub!(/<\/body>$/, "<!-- served to you through a copper wire by #{request.host} at #{Time.now.to_s(:short)} in #{total_runtime} ms (d #{db_runtime} / r #{rendering_runtime}). thank you, come again. -->\n</body>") 
    end
  end
end

